<?php
namespace catchAdmin\apipatient\controller;

use app\common\model\Project;
use catcher\CatchResponse;
use catcher\base\CatchController;
use app\common\model\PropagandaEducation as PropagandaEducationModel;
use app\common\model\PropagandaEducationClass as PropagandaEducationClassModel;
use app\common\model\PropagandaEducationHistory as PropagandaEducationHistoryModel;
use catchAdmin\apipatient\validate\EdValidate;
use think\exception\ValidateException;
use think\Request;
use catchAdmin\apipatient\service\EdService;
use app\common\model\PropagandaEducationSatisfaction as PESModel;


class Ed extends CatchController
{ 
    /**
     * 获取健康宣教分类
     *
     * @time 2020年01月09日
     * @return \think\response\Json
     */
    public function get_ed_class(): \think\response\Json
    {
        return CatchResponse::paginate((new PropagandaEducationClassModel())->getEdClass());
    }

    /**
     * 获取健康宣教
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_ed_list(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('get_ed_list')->check($req);
            return CatchResponse::paginate((new PropagandaEducationModel())->getEdDatas($req['classify']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取宣教满意度调查问卷
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_hzd_satisfied_quest(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('get_hzd_satisfied_quest')->check($req);
            $ret = (new PESModel())->getDatas(['classify' => $req['classify']]);
            $project = (new Project())->where('projectId', $ret[0]['projectId'])->field('answer_number')->find();
            $ret[0]['participants_num'] = $project['answer_number']??0;
            return CatchResponse::success($ret);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取我的宣教
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_ed_my(Request $request): \think\response\Json
    {
        #验证参数
        try {
            return CatchResponse::success((new PropagandaEducationHistoryModel())->getMyEd(request()->userInfo['patientId']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }   
    }

    /**
     * 健康养生知识
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_ed_jkys(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('get_ed_jkys')->check($req);

            //类型：1 更多，2 首页
            if ($req['type'] == 2)
            {
                return CatchResponse::success((new PropagandaEducationModel())->getJkysDatas($req['type']));
            }
            return CatchResponse::paginate((new PropagandaEducationModel())->getJkysDatas($req['type']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取宣教详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_ed_detail(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('get_ed_detail')->check($req);
            return CatchResponse::success((new PropagandaEducationModel())->getHzDetail($req['ed_id']));    
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取我的宣教详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_ed_detail(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('get_my_ed_detail')->check($req);
            return CatchResponse::success((new EdService())->EdDetail($req['id'], $req['type'], $req['ed_id']));    
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 我的宣教-住院宣教签名
     * @param Request $request
     * @return \think\response\Json
     */
    public function inhospital_ed_sign(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(EdValidate::class)->scene('inhospital_ed_sign')->check($req);
            return CatchResponse::success((new EdService())->inEdSign($req));  
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }
}